package com.epam.web_project.bank.command.admin_commands;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class MarkUnapprovedAccountDeleted implements Command {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		String resPage = props.getProperty("show_admin_main_command");
		if (request.getParameter("accId").equals("")) {
			return resPage;
		}
		Account account = new Account();
		account.setId(Integer.valueOf(request.getParameter("accId")));
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		AccountDAO accountDAO = daoFactory.getAccountDAO();
		String message;
		Locale locale=request.getLocale();
		ResourceBundle bundle=ResourceBundle.getBundle("i18n", locale);
		if(accountDAO.markDeleted(account)) {
			message=bundle.getString("java.acc_del");
		} else {
			message=bundle.getString("java.acc_del_err");
		}
		HttpSession session = request.getSession();
		session.setAttribute("message", message);
		return resPage;
	}

}
